package com.bhs.sansong.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.TypeAdapter;
import com.bhs.sansong.bean.TabBean;
import com.bhs.sansong.utils.ToastUtil;
import com.bhs.sansong.view.AfterTextChanged;
import com.bhs.sansong.view.MyTextWatcher;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 货物类型
 */
public class GoodTypeDialog extends Dialog implements View.OnClickListener {

    private RecyclerView mRvList;
    private EditText mEtOther;
    private EditText mEtCount;
    private EditText mEtWeight;
    private EditText mEtVolume;
    private List<String> data;
    private TypeAdapter typeAdapter;
    private int count = 0;
    private float weight = 0, volume = 0;
    private boolean isHaveChosen;//是否选了货物类型
    private boolean isPerfect;//货物类型是否完善
    private String goodType;
    private RelativeLayout mRlOther;
    private TextView mTvNumber;

    public GoodTypeDialog(@NonNull Context context, List<String> data) {
        super(context, R.style.MyBottomDialogStyle);
        this.data = data;
    }

    public GoodTypeDialog(@NonNull Context context) {
        super(context, R.style.MyBottomDialogStyle);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_good_type);
        Window window = getWindow();
        //底部弹出的Dialog
        window.setGravity(Gravity.BOTTOM);
        //设置动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams attributes = window.getAttributes();

        //保证按钮水平满屏
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        //设置显示位置
        window.setAttributes(attributes); //将属性设置给窗体
        initView();
    }

    private void initView() {
        findViewById(R.id.iv_close).setOnClickListener(this);
        mRvList = findViewById(R.id.rv_list);
        mEtOther = findViewById(R.id.et_other);
        findViewById(R.id.iv_count_minus).setOnClickListener(this);
        mEtCount = findViewById(R.id.et_count);
        findViewById(R.id.iv_count_plus).setOnClickListener(this);
        findViewById(R.id.iv_weight_minus).setOnClickListener(this);
        mEtWeight = findViewById(R.id.et_weight);
        findViewById(R.id.iv_weight_plus).setOnClickListener(this);
        findViewById(R.id.iv_volume_minus).setOnClickListener(this);
        mEtVolume = findViewById(R.id.et_volume);
        findViewById(R.id.iv_volume_plus).setOnClickListener(this);
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        mEtVolume.setOnClickListener(this);
        mEtWeight.setOnClickListener(this);
        mEtCount.setOnClickListener(this);
        if (count > 0) {
            mEtCount.setText(String.valueOf(count));
        }
        if (weight > 0) {
            mEtWeight.setText(String.valueOf(weight));
        }
        if (volume > 0) {
            mEtVolume.setText(String.valueOf(volume));
        }
        mRlOther = findViewById(R.id.rl_other);
        mTvNumber = findViewById(R.id.tv_number);
        List<TabBean> mList = new ArrayList<>();
        for (String datum : data) {
            if (!TextUtils.isEmpty(goodType) && datum.equals(goodType)) {
                mList.add(new TabBean(datum, true));
                isPerfect = true;
                isHaveChosen = true;
            } else {
                mList.add(new TabBean(datum));
            }
        }
        if (!TextUtils.isEmpty(goodType) && !isPerfect) {
            mList.get(mList.size() - 1).setChecked(true);
            mEtOther.setText(goodType);
            mRlOther.setVisibility(View.VISIBLE);
            isPerfect = true;
            isHaveChosen = true;
        }
        typeAdapter = new TypeAdapter(mList);
        mRvList.setAdapter(typeAdapter);
        mRvList.setLayoutManager(new GridLayoutManager(getContext(), 4));
        typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                isHaveChosen = true;
                for (TabBean datum : typeAdapter.getData()) {
                    datum.setChecked(false);
                }
                goodType = typeAdapter.getData().get(position).getTypeName();
                if (goodType.equals("其他")) {
                    mRlOther.setVisibility(View.VISIBLE);
                } else {
                    mRlOther.setVisibility(View.GONE);
                }
                typeAdapter.getData().get(position).setChecked(true);
                typeAdapter.notifyDataSetChanged();
            }
        });
        MyTextWatcher myTextWatcher = new MyTextWatcher(mEtCount).setMaxValue(10000);
        myTextWatcher.addTextChangedListener(new AfterTextChanged() {
            @Override
            public void onCallBack(String s) {
                count = Integer.parseInt(s);
            }
        });
        mEtCount.addTextChangedListener(myTextWatcher);
        MyTextWatcher myTextWatcher2 = new MyTextWatcher(mEtWeight).setMaxValue(100);
        myTextWatcher2.addTextChangedListener(new AfterTextChanged() {
            @Override
            public void onCallBack(String s) {
                weight = Float.parseFloat(s);
            }
        });
        mEtWeight.addTextChangedListener(myTextWatcher2);
        MyTextWatcher myTextWatcher3 = new MyTextWatcher(mEtVolume).setMaxValue(1000);
        myTextWatcher3.addTextChangedListener(new AfterTextChanged() {
            @Override
            public void onCallBack(String s) {
                volume = Float.parseFloat(s);
            }
        });
        mEtVolume.addTextChangedListener(myTextWatcher3);
        mEtOther.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                mTvNumber.setText(String.format("%s/10", editable.length()));
            }
        });
    }


    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.iv_count_minus:
                if (count > 1) {
                    count--;
                } else {
                    ToastUtil.show("不可以在减了");
                }
                mEtCount.setText(String.valueOf(count));
                mEtCount.setSelection(mEtCount.getText().toString().length());
                break;
            case R.id.iv_count_plus:
                count++;
                mEtCount.setText(String.valueOf(count));
                mEtCount.setSelection(mEtCount.getText().toString().length());
                break;
            case R.id.iv_weight_minus:
                minus(mEtWeight, weight);
                break;
            case R.id.iv_weight_plus:
                plus(mEtWeight, weight);
                break;
            case R.id.iv_volume_minus:
                minus(mEtVolume, volume);
                break;
            case R.id.iv_volume_plus:
                plus(mEtVolume, volume);
                break;
            case R.id.btn_confirm:
                if (!isHaveChosen) {
                    ToastUtil.show("请选择货物类型");
                    return;
                }
                if (goodType.equals("其他") && TextUtils.isEmpty(mEtOther.getText())) {
                    ToastUtil.show("请输入货物名称");
                    return;
                }
                if (TextUtils.isEmpty(mEtCount.getText())) {
                    ToastUtil.show("请输入数量");
                    return;
                }
                if (TextUtils.isEmpty(mEtWeight.getText())) {
                    ToastUtil.show("请输入重量");
                    return;
                }
                if (TextUtils.isEmpty(mEtVolume.getText())) {
                    ToastUtil.show("请输入体积");
                    return;
                }
                if (TextUtils.isEmpty(mEtVolume.getText())) {
                    ToastUtil.show("请输入体积");
                    return;
                }
                isPerfect = true;
                dismiss();
                if (listener != null) {
                    String id;
                    if (getWeight() >= 3 || getVolume() >= 9) {
                        id = "1";
                    } else {
                        id = "2";
                    }
                    listener.onClick(id, String.format("%s、%s件、%st、%sm³", getGoodType(), getCount(), getWeight(), getVolume()));
                }
                break;
        }
    }

    private void minus(EditText editText, float num) {
        if (num > 1) {
            num--;
        } else {
            ToastUtil.show("不可以在减了");
        }
        editText.setText(String.valueOf(num));
        editText.setSelection(editText.getText().toString().length());
    }

    private void plus(EditText editText, float num) {
        num++;
        editText.setText(String.valueOf(num));
        editText.setSelection(editText.getText().toString().length());
    }

    public int getCount() {
        return count;
    }

    public float getWeight() {
        return weight;
    }

    public float getVolume() {
        return volume;
    }

    public List<String> getData() {
        return data;
    }

    public String getGoodType() {
        if (goodType.equals("其他")) {
            return mEtOther.getText().toString();
        } else {
            return goodType;
        }
    }

    public void setGoodType(String goodType) {
        this.goodType = goodType;
    }

    public boolean isPerfect() {
        return isPerfect;
    }

    public void setPerfect(boolean perfect) {
        isPerfect = perfect;
    }

    private MyClickListener2 listener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.listener = l;
    }

    public void setData(List<String> data) {
        this.data = data;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }

    public void setVolume(float volume) {
        this.volume = volume;
    }
}
